{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Demo of STMatch Python API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from fmm import Network,NetworkGraph,STMATCH,STMATCHConfig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load network and graph data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Nodes 17 edges 30\n"
     ]
    }
   ],
   "source": [
    "network = Network(\"../data/edges.shp\")\n",
    "print \"Nodes {} edges {}\".format(network.get_node_count(),network.get_edge_count())\n",
    "graph = NetworkGraph(network)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create STMATCH model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = STMATCH(network,graph)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Define STMATCH configuration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "k = 4\n",
    "gps_error = 0.5\n",
    "radius = 0.4\n",
    "vmax = 30\n",
    "factor = 1.5\n",
    "stmatch_config = STMATCHConfig(k, radius, gps_error, vmax, factor)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Run map matching"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Match a single trajectory in wkt format"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "wkt = \"LINESTRING(0.200812146892656 2.14088983050848,1.44262005649717 2.14879943502825,3.06408898305084 2.16066384180791,3.06408898305084 2.7103813559322,3.70872175141242 2.97930790960452,4.11606638418078 2.62337570621469)\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = model.match_wkt(wkt,stmatch_config)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Matched path:  [8, 11, 13, 18, 20, 24]\n",
      "Matched edge for each point:  [8, 11, 18, 18, 20, 24]\n",
      "Matched edge index  [0, 1, 3, 3, 4, 5]\n",
      "Matched geometry:  LINESTRING(0.20081215 2,1 2,2 2,3 2,3 3,4 3,4 2.6233757)\n",
      "Matched point  LINESTRING(0.20081215 2,1.4426201 2,3 2.1606638,3 2.7103814,3.7087218 3,4 2.6233757)\n"
     ]
    }
   ],
   "source": [
    "print \"Matched path: \", list(result.cpath)\n",
    "print \"Matched edge for each point: \", list(result.opath)\n",
    "print \"Matched edge index \",list(result.indices)\n",
    "print \"Matched geometry: \",result.mgeom.export_wkt()\n",
    "print \"Matched point \", result.pgeom.export_wkt()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Match GPS data stored in a file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "from fmm import GPSConfig,ResultConfig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define input data configuration\n",
    "input_config = GPSConfig()\n",
    "input_config.file = \"../data/trips.csv\"\n",
    "input_config.id = \"id\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "gps file : ../data/trips.csv\n",
      "id column : id\n",
      "geom column : geom\n",
      "timestamp column : timestamp\n",
      "x column : x\n",
      "y column : y\n",
      "GPS point : false\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print input_config.to_string()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Result file : ../data/mr.txt\n",
      "Output fields: opath cpath mgeom \n"
     ]
    }
   ],
   "source": [
    "# Define output configuration\n",
    "result_config = ResultConfig()\n",
    "result_config.file = \"../data/mr.txt\"\n",
    "result_config.output_config.write_opath = True\n",
    "print result_config.to_string()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "status = model.match_gps_file(input_config, result_config, stmatch_config)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Time takes 0.001 seconds\n",
      "Total points 17 matched 17\n",
      "Map match speed 17000 points/s \n",
      "\n"
     ]
    }
   ],
   "source": [
    "print status"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.16"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
